package com.canbot.u05.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.canbot.u05.R;

import java.util.HashMap;
import java.util.List;


public class GridViewAdapter extends BaseAdapter {

        private List<HashMap<String, String>> myList;//List<HashMap<String, Object>> list

        private Context mContext;

        private TextView name_tv;

        private ImageView img;

        private View deleteView;

        private boolean isShowDelete;//根据这个变量来判断是否显示删除图标，true是显示，false是不显示

        public void setMyList(List<HashMap<String, String>> myList) {
                this.myList = myList;
        }

        public List<HashMap<String, String>> getMyList() {
                return myList;
        }

        public GridViewAdapter(Context mContext, List<HashMap<String, String>> myList) {
                this.mContext = mContext;
                this.myList = myList;
        }

        public void setIsShowDelete(boolean isShowDelete) {
                this.isShowDelete = isShowDelete;
                notifyDataSetChanged();
        }

        @Override
        public int getCount() {

                return myList.size();
        }

        @Override
        public Object getItem(int position) {
                // TODO Auto-generated method stub
                return myList.get(position);
        }

        @Override
        public long getItemId(int position) {
                // TODO Auto-generated method stub
                return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
                ViewHolder mHolder;
                if (convertView == null) {
                        mHolder = new ViewHolder();
                        convertView = LayoutInflater.from(mContext).inflate(
                                R.layout.item_drag_delete, null);
                        mHolder.name_tv = (TextView) convertView.findViewById(R.id.tvDragitemLable);
                        convertView.setTag(mHolder);
                }
                else {
                        mHolder = (ViewHolder) convertView.getTag();
                }


//        deleteView.setVisibility(isShowDelete ? View.VISIBLE : View.INVISIBLE);//设置删除按钮是否显示
                HashMap<String, String> hashMap = myList.get(position);
                String text = hashMap.get("text").toString();
                String dance = "";
                String type = hashMap.get("type");
                if (!TextUtils.isEmpty(type)) {
                        if ("tts".equals(type)) {
                                String result = hashMap.get("result");
                                dance = result.length() > 4 ? result.substring(0, 4) + "..." : result;
                        }
                        else {
                                dance = hashMap.get("result");
                        }
                }
                mHolder.name_tv.setText(text + (TextUtils.isEmpty(dance) ? "" : "\n(" + dance + ")"));
//        deleteView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                myList.remove(position);
//                notifyDataSetChanged();
//                Log.i("ii", "da:" + myList);
//            }
//        });
                return convertView;
        }

        public class ViewHolder {

                public TextView name_tv;
        }


}
